﻿using LowCoding.Common;
using Microsoft.AspNetCore.Mvc;

namespace LowCoding.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BaseController : ControllerBase
    {
        #region 统一返回值
        /// <summary>
        /// 根据返回bool类型返回成功或失败
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        [NonAction]
        public static ResponseMessageModel ResultByBool(bool result)
        {
            if (result)
            {
                return Success();
            }
            else
            {
                return Fail("操作失败");
            }
        }

        /// <summary>
        /// 返回成功
        /// </summary>
        /// <returns></returns>


        public static ResponseMessageModel Success()
        {
            return ToJsonTran.Success(null);
        }
        /// <summary>
        /// 表示这个方法不被公开
        /// </summary>
        /// <returns></returns>
        [NonAction]
        public ResponseMessageModel test()
        {
            return ToJsonTran.Success(null);
        }
        /// <summary>
        /// 返回成功包含数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [NonAction]
        public static ResponseMessageModel Success(object data)
        {
            return ToJsonTran.Success(data);
        }

        /// <summary>
        /// 返回失败
        /// </summary>
        /// <param name="code"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        [NonAction]
        public static ResponseMessageModel Fail(int code, string message)
        {
            return ToJsonTran.Fail(code, message, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        [NonAction]
        public static ResponseMessageModel Fail(string message)
        {
            return ToJsonTran.Fail(ResponseCodeEnum.Fail, message, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        [NonAction]
        public static ResponseMessageModel Fail(ResponseCodeEnum code, string message)
        {
            return ToJsonTran.Fail((int)code, message, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        [NonAction]
        public static ResponseMessageModel Fail(ResponseCodeEnum code, string message, object data)
        {
            return ToJsonTran.Fail(code, message, data);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        [NonAction]
        public static ResponseMessageModel Fail(int code, string message, object data)
        {
            return ToJsonTran.Fail(code, message, data);
        }

        #endregion 统一返回值

    }
}
